package com.sugar.follow.up.module.business.controller.admin.questionnairetemplate;

import com.sugar.follow.up.framework.security.core.util.SecurityFrameworkUtils;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.sugar.follow.up.framework.common.pojo.PageParam;
import com.sugar.follow.up.framework.common.pojo.PageResult;
import com.sugar.follow.up.framework.common.pojo.CommonResult;
import com.sugar.follow.up.framework.common.util.object.BeanUtils;
import static com.sugar.follow.up.framework.common.pojo.CommonResult.success;

import com.sugar.follow.up.framework.excel.core.util.ExcelUtils;

import com.sugar.follow.up.framework.apilog.core.annotation.ApiAccessLog;
import static com.sugar.follow.up.framework.apilog.core.enums.OperateTypeEnum.*;

import com.sugar.follow.up.module.business.controller.admin.questionnairetemplate.vo.*;
import com.sugar.follow.up.module.business.dal.dataobject.questionnairetemplate.QuestionnaireTemplateDO;
import com.sugar.follow.up.module.business.service.questionnairetemplate.QuestionnaireTemplateService;

@Tag(name = "管理后台 - 问卷模板")
@RestController
@RequestMapping("/business/questionnaire-template")
@Validated
public class QuestionnaireTemplateController {

    @Resource
    private QuestionnaireTemplateService questionnaireTemplateService;

    @PostMapping("/create")
    @Operation(summary = "创建问卷模板")
    @PreAuthorize("@ss.hasPermission('business:questionnaire-template:create')")
    public CommonResult<Long> createQuestionnaireTemplate(@Valid @RequestBody QuestionnaireTemplateSaveReqVO createReqVO) {
        return success(questionnaireTemplateService.createQuestionnaireTemplate(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新问卷模板")
    @PreAuthorize("@ss.hasPermission('business:questionnaire-template:update')")
    public CommonResult<Boolean> updateQuestionnaireTemplate(@Valid @RequestBody QuestionnaireTemplateSaveReqVO updateReqVO) {
        questionnaireTemplateService.updateQuestionnaireTemplate(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除问卷模板")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('business:questionnaire-template:delete')")
    public CommonResult<Boolean> deleteQuestionnaireTemplate(@RequestParam("id") Long id) {
        questionnaireTemplateService.deleteQuestionnaireTemplate(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除问卷模板")
                @PreAuthorize("@ss.hasPermission('business:questionnaire-template:delete')")
    public CommonResult<Boolean> deleteQuestionnaireTemplateList(@RequestParam("ids") List<Long> ids) {
        questionnaireTemplateService.deleteQuestionnaireTemplateListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得问卷模板")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('business:questionnaire-template:query')")
    public CommonResult<QuestionnaireTemplateRespVO> getQuestionnaireTemplate(@RequestParam("id") Long id) {
        QuestionnaireTemplateDO questionnaireTemplate = questionnaireTemplateService.getQuestionnaireTemplate(id);
        return success(BeanUtils.toBean(questionnaireTemplate, QuestionnaireTemplateRespVO.class));
    }

    @GetMapping("/all")
    @Operation(summary = "获得所有问卷模板")
    public CommonResult<List<QuestionnaireTemplateSimpleRespVO>> getQuestionnaireTemplateAll() {
        return success(questionnaireTemplateService.getQuestionnaireTemplateAll());
    }

    @GetMapping("/page")
    @Operation(summary = "获得问卷模板分页")
    @PreAuthorize("@ss.hasPermission('business:questionnaire-template:query')")
    public CommonResult<PageResult<QuestionnaireTemplateRespVO>> getQuestionnaireTemplatePage(@Valid QuestionnaireTemplatePageReqVO pageReqVO) {
        PageResult<QuestionnaireTemplateDO> pageResult = questionnaireTemplateService.getQuestionnaireTemplatePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, QuestionnaireTemplateRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出问卷模板 Excel")
    @PreAuthorize("@ss.hasPermission('business:questionnaire-template:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportQuestionnaireTemplateExcel(@Valid QuestionnaireTemplatePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        questionnaireTemplateService.exportQuestionnaireTemplateExcel(pageReqVO, response);
    }

}